import {createStore, Store} from 'vuex';
import router from "@/router";

export default createStore({
    state: {
        hasRoutes: false,
        editableTabsValue: '/index',
        editableTabs: [
            {
                title: '首页',
                name: '/index'
            }
        ],
        token: '',
        userInfo: {},
        menuList: {}

    },
    getters: {
        GET_TOKEN: (state: any) => {
            return state.token
        },
        GET_MENU_LIST: (state: any) => {
            return state.menuList
        },
        GET_USER_INFO: (state: any) => {
            return state.userInfo
        },
        GET_HAS_ROUTES: (state: any) => {
            return state.hasRoutes
        }
    },
    mutations: {
        SET_TOKEN: (state:any, token:any) => {
            state.token = token
            localStorage.setItem('Authorization', token);
        },
        SET_MENU_LIST: (state:any, menuList:any) => {
            state.menuList = menuList
            localStorage.setItem("menuList", JSON.stringify(menuList));

        },
        SET_USER_INFO: (state:any, userInfo:any) => {
            state.userInfo = userInfo
            localStorage.setItem("userInfo", JSON.stringify(userInfo));
        },
        SET_ROUTES_STATE: (state:any, hasRoutes:any) => {
            state.hasRoutes = hasRoutes;
        },
        ADD_TABS: (state:any, tab:any) => {
            if (state.editableTabs.findIndex((e:any) => e.name === tab.path) === -1) {
                state.editableTabs.push({
                    title: tab.name,
                    name: tab.path
                })
            }
            state.editableTabsValue = tab.path
        },
        RESET_TABS: (state:any) => {
            state.editableTabsValue = '/index',
                state.editableTabs = [
                    {
                        title: '首页',
                        name: '/index'
                    }
                ]
        }

    },
    actions: {
        //安全退出
        logout() {
            localStorage.clear();
            router.push("/login")
        }

    },
    modules: {}
})
